package cn.xgxd.service.Impl;
import cn.xgxd.client.UserClient;
import cn.xgxd.mapper.ForumMapper;
import cn.xgxd.pojo.Forum;
import cn.xgxd.result.Result;
import cn.xgxd.service.ForumService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class ForumServiceImpl implements ForumService {

    @Autowired
    private ForumMapper forumMapper;
    @Autowired
    private UserClient userClient;


    @Override
    public Map<String, Object> findAllpost(Integer page) {
        Map<String,Object> map = new HashMap<>();
        Page<Forum> pages = new Page<>(page, 10);
        forumMapper.selectPage(pages, null);
        map.put("data",pages.getRecords());
        map.put("count",pages.getTotal());
        return map;
    }

    @Override
    public boolean insertForum(Forum forum) {
        Forum f = new Forum();
        f.setName(forum.getName());
        f.setTime(forum.getTime());
        f.setUserName(forum.getUserName());
        forumMapper.insert(f);
        return true;
    }

    @Override
    public Map findUserById(String forumId) {
        Map<String,Object> map = new HashMap<>();
        Forum forum = forumMapper.selectById(forumId);
        Result byId = userClient.findById(forum.getUserId());
        map.put("forum",forum);
        map.put("user",byId.getData());
        return map;
    }

    @Override
    public int selectForunCount() {
        return forumMapper.selectCount(null);
    }
}
